package com.zw.controller.admin.tools;

import com.zw.annotation.Log;
import com.zw.core.controller.BaseController;
import com.zw.core.domain.AjaxResult;
import com.zw.core.page.TableDataInfo;
import com.zw.entity.Template;
import com.zw.enumeration.BusinessType;
import com.zw.service.TemplateService;
import com.zw.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板管理
 *
 * @author zw
 */
@RestController
@RequestMapping("/tools/template")
public class PtTemplateController extends BaseController {

    @Resource
    private TemplateService templateService;

    /**
     * 获取模板列表
     */
    @PreAuthorize("@ss.hasPermi('tools:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(Template template) {
        startPage();
        List<Template> list = templateService.selectTemplateList(template);
        return getDataTable(list);
    }

    /**
     * 查询模板下拉列表
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:template:query')")
    @GetMapping("/optionselect")
    public AjaxResult optionselect() {
        return success(templateService.list());
    }

    /**
     * 导出模板列表
     *
     * @param response
     * @param template
     */
    @PreAuthorize("@ss.hasPermi('tools:template:export')")
    @Log(title = "模板管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Template template) {
        List<Template> list = templateService.selectTemplateList(template);
        ExcelUtil<Template> util = new ExcelUtil<>(Template.class);
        util.exportExcel(response, list, "模板数据");
    }

    /**
     * 获取模板详细信息
     *
     * @param templateId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:template:query')")
    @GetMapping("/{templateId}")
    public AjaxResult getInfo(@PathVariable Long templateId) {
        return AjaxResult.success(templateService.selectTemplateById(templateId));
    }

    /**
     * 新增模板
     *
     * @param template
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:template:add')")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Template template) {
        return toAjax(templateService.insertTemplate(template));
    }

    /**
     * 修改模板
     *
     * @param template
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:template:edit')")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Template template) {
        return toAjax(templateService.updateTemplate(template));
    }

    /**
     * 删除模板
     *
     * @param templateIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:template:remove')")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds) {
        return toAjax(templateService.removeTemplates(templateIds));
    }
}
